//
//  EFengShareNewCommentCell.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/15.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengShareNewCommentCell.h"
#import "EFengShareNewCommentFrame.h"
#import "EFengContent.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "CXmppRequestMgr.h"
#import "UIUtil.h"
#import "UIImage+ImageWithColor.h"

@interface EFengShareNewCommentCell ()

WeakProperty UIButton *headIcon;
WeakProperty UILabel *nameLabel;
WeakProperty UILabel *timeLabel;
WeakProperty UILabel *contentLabel;
WeakProperty UILabel *shareContentLabel;
WeakProperty UIView *line;
WeakProperty UIButton *zanBtn;
WeakProperty UIImageView *picView;

@end

@implementation EFengShareNewCommentCell

+ (EFengShareNewCommentCell *)eFengShareNewCommentCellWithTableView:(UITableView *)tableView
{
    static NSString *shareNewCommentCellId = @"shareNewCommentCell";
    EFengShareNewCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:shareNewCommentCellId];
    if (cell == nil) {
        cell = [[EFengShareNewCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:shareNewCommentCellId];
    }
    return cell;

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = WhiteColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
        img.titleLabel.numberOfLines = 0;
        img.titleLabel.textAlignment = UITextAlignmentCenter;
        
        img.userInteractionEnabled = YES;
        img.layer.cornerRadius = 3.0f;
        img.layer.masksToBounds = YES;
        [self.contentView addSubview:img];
        self.headIcon = img;
        
        
        UIButton *zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        zanBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        zanBtn.userInteractionEnabled = NO;
        [zanBtn setImage:[UIImage imageNamed:@"btn-good"] forState:UIControlStateNormal];
        [self.contentView addSubview:zanBtn];
        self.zanBtn = zanBtn;

        
        UILabel *name = [[UILabel alloc] init];
        name.textAlignment = UITextAlignmentLeft;
        name.font = Font14;
        name.textColor = RGBCOLORWITH16COLOR(0x323232);
        [self.contentView addSubview:name];
        self.nameLabel = name;
        
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.textAlignment = UITextAlignmentLeft;
        timeLabel.font = Font12;
        timeLabel.textColor = RGBCOLORWITH16COLOR(0xd4d4d4);
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.numberOfLines = 0;
        contentLabel.textAlignment = UITextAlignmentLeft;
        contentLabel.font = Font13;
        contentLabel.textColor = RGBCOLORWITH16COLOR(0x929292);
        [self.contentView addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
        UILabel *shareContentLabel = [[UILabel alloc] init];
        shareContentLabel.numberOfLines = 0;
        shareContentLabel.textAlignment = UITextAlignmentLeft;
        shareContentLabel.font = Font13;
        shareContentLabel.textColor = RGBCOLORWITH16COLOR(0x929292);
        [self.contentView addSubview:shareContentLabel];
        self.shareContentLabel = shareContentLabel;

        
        UIImageView *picView = [[UIImageView alloc] init];
        picView.backgroundColor = ClearColor;
        [self.contentView addSubview:picView];
        self.picView = picView;
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = RGBCOLOR(212, 212, 212);
        [self.contentView addSubview:line];
        self.line = line;
    }
    return self;
}

- (void)setUnReadCommentFrame:(EFengShareNewCommentFrame *)unReadCommentFrame
{
    _unReadCommentFrame = unReadCommentFrame;
    EFengContent *unReadComment = unReadCommentFrame.unReadComment;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:unReadComment.date.longLongValue / 1000.0];
    
    [self.headIcon sd_setBackgroundImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:unReadComment.sendUserId]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"80-80"]];

    self.nameLabel.text = unReadComment.sendUserName;
    self.timeLabel.text = [UIUtil timeForNewCommentListStyle:date];
        
    self.headIcon.frame = unReadCommentFrame.headFrame;
    
    
    self.nameLabel.frame = unReadCommentFrame.nameFrame;

    self.timeLabel.frame = unReadCommentFrame.timeFrame;
    
    if ([unReadComment.commentType isEqualToString:@"praise"]) {
        
        self.zanBtn.hidden = NO;
        self.contentLabel.hidden = YES;
    } else {
        self.zanBtn.hidden = YES;
        self.contentLabel.hidden = NO;
    }
    
    self.zanBtn.frame = unReadCommentFrame.zanFrame;
    self.contentLabel.text = unReadComment.content;
    self.contentLabel.frame = unReadCommentFrame.contentFrame;
    
    if (unReadComment.imageName.count > 0) {
        NSDictionary *obj = unReadComment.imageName.firstObject;
        [self.picView sd_setImageWithURL:[NSURL URLWithString:[obj stringForKey:@"url_min"]] placeholderImage:[UIImage createImageWithColor:RGBCOLOR(212, 212, 212) withBounds:self.picView.bounds]];
        self.picView.frame = unReadCommentFrame.picViewFrame;
        self.shareContentLabel.hidden = YES;
        self.picView.hidden = NO;
    } else {
        self.shareContentLabel.text = unReadComment.shareContent;
        self.shareContentLabel.frame = unReadCommentFrame.shareContentFrame;
        self.shareContentLabel.hidden = NO;
        self.picView.hidden = YES;

    }
    
    self.line.frame = unReadCommentFrame.lineFrame;

}

@end
